import { createRouter, createWebHashHistory } from 'vue-router'
import store from '@/store'
// import Home from '../views/Home.vue'
const Layout = () => import('@/views/Layout')
const Home = () => import('@/views/Home/index')
const routes = [
  {
    path: '/',
    name: 'Layout',
    component: Layout,
    children: [
      { path: '/', component: Home },
      // '/category/ :id'动态路由情况
      { path: '/category/:id', component: () => import('@/views/Category') },
      {
        path: '/category/sub/:id', // 动态路由 $route.params.id
        name: 'subCategory',
        component: () => import(/* webpackChunkName: "about" */ '../views/Category/sub.vue')
      },
      {
        path: '/goods/:id', // 动态路由 $route.params.id
        name: 'goods',
        component: () => import(/* webpackChunkName: "about" */ '../views/Goods/index.vue')
      },
      {
        path: '/cart',
        name: 'cart',
        component: () => import(/* webpackChunkName: "cart" */ '../views/Cart/index.vue')
      },
      {
        path: '/member/checkout',
        component: () => import('@/views/Member/pay/checkout')
      }

    ]
  },
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
  {
    path: '/Playground',
    name: 'Playground',
    component: () => import('../views/Playground/index.vue')

  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login/index.vue')
  },
  { path: '/login/callback', component: () => import('@/views/Login/callback.vue') }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 前置全局路由导航守卫、
router.beforeEach((to, from, next) => {
  // 如果当前的地址以/member开头，则必须要token
  if (to.path.startsWith('/member')) {
    // 是否有token
    if (store.state.user.profile.token) {
      next()
    } else {
      next('/login?return_url=' + to.fullPath)
    }
  } else {
    next()
  }
})
export default router
